package the.workshop.bu.dto.req;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import the.broccoli.auth.UserContext;
import the.workshop.bu.entity.Customer;
import the.workshop.bu.entity.CustomerTel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
public class CustomerAddReq {

    /**
     * 客户名称
     */
    private String name;

    /**
     * 法人
     */
    private String legal;
    private String idNumber;
    private String wxNumber;
    private Date followTime;

    /**
     * 备注
     */
    private String remark;
    private String status;

    /**
     * 联系电话
     */
    private String tels;

    public Customer toCustomer() {
        Customer customer = new Customer();
        BeanUtils.copyProperties(this, customer);
        customer.setSalesId(UserContext.userId());
        customer.setCreatedTime(new Date());
        customer.setCreatedId(UserContext.userId());
        customer.setCreatedName(UserContext.userName());
        customer.setStatusTime(new Date());
        return customer;
    }

    public List<CustomerTel> toCustomerTels(Integer customerId) {
        if (!StringUtils.hasText(tels)) {
            return null;
        }

        List<CustomerTelAddReq> list = JSON.parseArray(tels, CustomerTelAddReq.class);
        List<CustomerTel> records = new ArrayList<>();
        CustomerTel record = null;
        for (CustomerTelAddReq tel : list) {
            record = new CustomerTel();
            record.setCustomerId(customerId);
            record.setTelNumber(tel.getTelNumber());
            record.setIsLegal(tel.getIsLegal());
            record.setCreatedTime(new Date());
            record.setCreatedId(UserContext.userId());
            record.setCreatedName(UserContext.userName());
            records.add(record);
        }
        return records;
    }
}
